# -*- coding:utf-8 -*-
# created on 2017/4/30
# 

from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex


# 更新正方形边长: 已知正方形ABCD的边长为2
class XLSquareLengthUpdate(BaseFunction):
    """
    已知正方形ABCD的边长为2,E为CD的中点,则\\overrightarrow{AE}•\\overrightarrow{BD}=().
    """
    def solver(self, *args):
        assert len(args) == 2
        p1, p2, p3, p4 = args[0].value
        name = p1 + p2 + p3 + p4
        assert name in self.known
        quadra = self.search(name)
        assert quadra
        assert quadra.type == 'vSquare'
        edge_length = args[1].sympify()
        if edge_length.free_symbols:
            v_eqs = quadra.Eqs
            v_eqs.append([edge_length, ">", S.Zero])
            quadra.Eqs = v_eqs
        quadra.edge_length = edge_length
        quadra.line_AB_value = edge_length
        quadra.line_BC_value = edge_length
        quadra.line_CD_value = edge_length
        quadra.line_DA_value = edge_length
        self.output.append(quadra)
        return self


# E为CD的中点
class XLQuadraZhongDianUpdate(BaseFunction):
    """
    已知正方形ABCD的边长为2,E为CD的中点,则\\overrightarrow{AE}•\\overrightarrow{BD}=().
    """
    def solver(self, *args):
        assert len(args) == 3
        p1, p2, p3, p4 = args[0].value
        name = p1 + p2 + p3 + p4
        assert name in self.known
        quadra = self.search(name)
        assert quadra
        p = args[1].sympify()
        line_left, line_right = args[2].value
        vector_a = "vector" + "(" + line_left + str(p) + ")"
        vector_a = sympify(vector_a)
        vector_b = "vector" + "(" + str(p) + line_right + ")"
        vector_b = sympify(vector_b)
        v_eqs = quadra.Eqs
        v_eqs.append([vector_a, vector_b])
        quadra.Eqs = v_eqs
        self.output.append(quadra)
        self.steps.append(["", "∵ %s是%s的中点" % (p, line_left+line_right)])
        self.steps.append(["", "∴ %s" % BaseEq([vector_a, vector_b]).printing()])
        return self


# 更新菱形边长: 已知菱形ABCD的边长为a
class XLDiamondLengthUpdate(BaseFunction):
    """
    已知菱形ABCD的边长为a,\\angle ABC=60°,则\\overrightarrow{BD}•\\overrightarrow{CD}=()
    """
    def solver(self, *args):
        assert len(args) == 2
        p1, p2, p3, p4 = args[0].value
        name = p1 + p2 + p3 + p4
        assert name in self.known
        diamond = self.search(name)
        assert diamond
        assert diamond.type == 'vDiamond'
        edge_length = args[1].sympify()
        if edge_length.free_symbols:
            v_eqs = diamond.Eqs
            v_eqs.append([edge_length, ">", S.Zero])
            diamond.Eqs = v_eqs
        diamond.edge_length = edge_length
        diamond.line_AB_value = edge_length
        diamond.line_BC_value = edge_length
        diamond.line_CD_value = edge_length
        diamond.line_DA_value = edge_length
        self.output.append(diamond)
        return self


# \\angle ABC=60°
class XLQuadraGeoEqUpdate(BaseFunction):
    """
    已知菱形ABCD的边长为a,\\angle ABC=60°,则\\overrightarrow{BD}•\\overrightarrow{CD}=()
    """
    def solver(self, *args):
        assert len(args) == 2
        p1, p2, p3, p4 = args[0].value
        name = p1 + p2 + p3 + p4
        assert name in self.known
        quadra = self.search(name)
        assert quadra
        eq_left = sympify(args[1].value[0])
        eq_right = sympify(args[1].value[1])
        assert str(eq_left.func) == str("Angle")
        assert str(eq_right.func) == str("du")
        angle_name = eq_left.args[1]
        angle_value = eq_right.args[0] / 180 * pi
        if str(angle_name) == quadra.point_A_name:
            quadra.Angle_A_value = angle_value
        elif str(angle_name) == quadra.point_B_name:
            quadra.Angle_B_value = angle_value
        elif str(angle_name) == quadra.point_C_name:
            quadra.Angle_C_value = angle_value
        elif str(angle_name) == quadra.point_D_name:
            quadra.Angle_D_value = angle_value
        else:
            raise 'try fail'
        self.output.append(quadra)
        return self


# 更新四边形方程
class XLQuadraEqUpdate(BaseFunction):
    def solver(self, *args):
        assert len(args) == 2
        p1, p2, p3, p4 = args[0].value
        name = p1 + p2 + p3 + p4
        assert name in self.known
        quadra = self.search(name)
        assert quadra
        quadra.eq_update(args[1])
        self.output.append(quadra)
        return self


# 更新四边形方程组
class XLQuadraEqsUpdate(BaseFunction):
    """
    已知矩形ABCD,|\\overrightarrow{AB}|=6, |\\overrightarrow{AD}|=4.\\overrightarrow{BM}=3\\overrightarrow{MC},
    \\overrightarrow{DN}=2\\overrightarrow{NC},则\\overrightarrow{AM}•\\overrightarrow{NM}=()
    """
    def solver(self, *args):
        assert len(args) == 2
        p1, p2, p3, p4 = args[0].value
        name = p1 + p2 + p3 + p4
        assert name in self.known
        quadra = self.search(name)
        assert quadra
        quadra.eqs_update(args[1])
        self.output.append(quadra)
        return self


# 更新两向量垂直
class XLQuadraVectorSChuiZhiUpdate(BaseFunction):
    def solver(self, *args):
        assert len(args) == 3
        p1, p2, p3, p4 = args[0].value
        name = p1 + p2 + p3 + p4
        assert name in self.known
        quadra = self.search(name)
        assert quadra
        if isinstance(args[1], BaseVector):
            vector_a = sympify(args[1].value)
        else:
            vector_a = args[1].sympify()
        if isinstance(args[2], BaseVector):
            vector_b = sympify(args[2].value)
        else:
            vector_b = args[2].sympify()
        eq = [vector_a * vector_b, S.Zero]
        quadra.Eqs.append(eq)
        self.steps.append(["", "∵ %s垂直%s," % (new_latex(vector_a), new_latex(vector_b))])
        self.steps.append(["", "∴ %s" % BaseEq(eq).printing()])
        self.output.append(quadra)
        return self
